﻿#include"GameMain.h"
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
CGameMain::CGameMain(HINSTANCE hInstance, LPWSTR Name, int Mode, int IsFullScreen, int FrameRate):CGameManager(hInstance,Name,Mode,IsFullScreen,FrameRate)
{
	sprite=NULL;
	Mario=NULL;
	Mushroom=NULL;
	items=NULL;
	FinishGame=NULL;
	image_for_sights=NULL;
	image_for_static_objects=NULL;
	image_for_enemies=NULL;
	image_for_menu=NULL;
	image_for_coins=NULL;
	Menu=NULL;
	text=NULL;
	menuchoose=NULL;
	level1sound=NULL;
	number=NULL;
	isMenu=true;
	isIntroduce=false;
	index=0;
	stage=0;
	score=0;
	coins=0;
}
CGameMain::~CGameMain()
{
	if(sprite!=NULL) sprite->Release();
	if(items!=NULL) delete items;
	if(Menu!=NULL) Menu->Release();
	if(image_for_menu!=NULL) image_for_menu->Release();
	if(image_for_sights!=NULL) image_for_sights->Release();
	if(image_for_static_objects!=NULL) image_for_static_objects->Release();
	if(image_for_enemies!=NULL) image_for_enemies->Release();
	if(image_for_coins!=NULL) image_for_coins->Release();
	if(Mario!=NULL) delete Mario;
	if(map!=NULL) delete map;
	if(text!=NULL) delete text;
	if(Mushroom!=NULL) delete Mushroom;
	if(FinishGame!=NULL) delete FinishGame;
	if(menuchoose!=NULL) delete menuchoose;
	if(level1sound!=NULL) delete level1sound;
	//if(list!=NULL) delete list;
}
void CGameMain::LoadResources(LPDIRECT3DDEVICE9 d3ddv)
{
	
	HRESULT result=D3DXCreateSprite(d3ddv,&sprite);
	if(result!=D3D_OK) return ;
	
	image_for_menu=LoadTexture(L"Image\\MushroomMenu.png");
	image_for_sights=LoadTexture(L"Image\\khungcanh.png");
	image_for_static_objects=LoadTexture(L"Image\\doituongtinh.png");
	image_for_coins=LoadTexture(L"Image\\coin.png");
	image_for_enemies=LoadTexture(L"Image\\enemies.png");
	image_for_items= LoadTexture(L"Image\\Items.png");
	menuchoose=LoadSound(L"Sound/click.wav");
	level1sound=LoadSound(L"Sound/level1.wav");
	level2sound=LoadSound(L"Sound/level2.wav");
	
	nSights=nObjects=nItems=0;

	
	Mushroom= new CMushroomMenu();
	text= new CText();
	text->LoadResources(d3ddv);
	
	number=new CNumber();
	number->LoadResources(d3ddv);
	items=new CItems();
//	tam=quadtree->nNode;
	list.pHead=NULL;
	list.pTail=NULL;
	MenuStage();
	//list_enemies.pHead=NULL;
	//list_enemies.pTail=NULL;
	//map->GetObjectFromQuadTree(list,camara->rect);
	//quadtree->getObject(quadtree->root,list,camara->rect);
		
	//for(int i=0;i<nSights;i++) ArraySights[i]->LoadResource(d3ddv);

}
void CGameMain::getNoOfObject()
{
	NODE *p;
	p=list.pHead;
	//AddListEnemies(p->object);
	while(p!=NULL)
	{
		switch(p->object.id)
		{
		case 0: nSights++;break;
		case 1: break;
		case 2: break;
		case 3: break;
		}
		p=p->next;
	}
	//if(p!=NULL) delete p;
}
void CGameMain::getListObject()
{
	int nS=0,nO=0,nE=0,nI=0;
	getNoOfObject();
	//ArrayItems=(CItems **)malloc(sizeof(CItems *)*nItems);
	ArraySights=(CSights **)malloc(sizeof(CSights *)*nSights);
	//ArrayObjects=(CStaticObjects **)malloc(sizeof(CStaticObjects *)*nObjects);
//	ArrayEnemies=(CEnemies **)malloc(sizeof(CEnemies *)*nEnemies);
	NODE *p;
	p=list.pHead;
	while(p!=NULL)
	{
		switch(p->object.id)
		{
		case 0: 
			{
				ArraySights[nS++]= new CSights(p->object.kind,p->object.x,p->object.y);
				
			}break;
		case 1: StaticObjects->GetStaticObject(p->object);break;
		case 2: Enemies->GetEnemies(p->object);break;
		case 3:	items->GetItems(p->object); break;
		case 4: 
			{
				if(FinishGame==NULL) FinishGame=new CFinish();
				FinishGame->GetObjects(p->object);
			}break;
		}
		p=p->next;
	}
	//if(p!=NULL) delete p;
}
void CGameMain::EmptyGame()
{
	Enemies->DeleteEnemies();
	StaticObjects->DeleteStaticObject();
	items->DeleteItems();
	map->EmptyQuadTree();
	
}
void CGameMain::EmptyList()
{
	for(int i=0;i<nSights;i++)  delete ArraySights[i];
	free(ArraySights);
	NODE *p;
	while(list.pHead!=NULL)
	{
		p=list.pHead;
		list.pHead=p->next;
		delete p;
	}

	nSights=0;
	nObjects=0;
	//nEnemies=0;
	nItems=0;
}
LPDIRECT3DSURFACE9 CGameMain::LoadSurface(LPWSTR filename)
{
	LPDIRECT3DSURFACE9 image;
	D3DXIMAGE_INFO info;
	HRESULT result= D3DXGetImageInfoFromFile(filename,&info);
	if(result!=D3D_OK)
	{
		return NULL;
	}
	d3ddv->CreateOffscreenPlainSurface(info.Width,info.Height,D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,&image,NULL);
	result= D3DXLoadSurfaceFromFile(image,NULL,NULL,filename,NULL,D3DX_DEFAULT,D3DCOLOR_XRGB(0,0,0),NULL);
	if( result!=D3D_OK)
	{
		return NULL ;
	}
	return image;	
}
LPDIRECT3DTEXTURE9 CGameMain::LoadTexture(LPWSTR Filename)
{
	D3DXIMAGE_INFO info;
	LPDIRECT3DTEXTURE9 image;
	HRESULT result=D3DXGetImageInfoFromFile(Filename,&info);
	if(result!=D3D_OK)
	{
		return NULL ;
	}
	D3DXCreateTextureFromFileEx(d3ddv,Filename,info.Width,info.Height,1,D3DUSAGE_DYNAMIC,D3DFMT_UNKNOWN,D3DPOOL_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,0,&info,NULL,&image);
	return image;
}
void CGameMain::InitGame()
{
	camara= new CCamara();
	Enemies= new CEnemies();
	StaticObjects=new CStaticObjects(map->width,map->height);
	
	
}
void CGameMain::MenuStage()
{
	map= new CMap(0);
	map->LoadMap();
	Mario= new CMario();
	Mario->LoadReSource(d3ddv);
	InitGame();
	timeplay=0;
	score=0;
	coins=0;
	stage=0;
	Mario->x=20;
	Mario->y=385;
	//LoopSound(menusound);
}
void CGameMain::Level1()
{
	EmptyGame();
	map= new CMap(1);
	map->LoadMap();
	InitGame();
	timeplay=300;
	stage=1;
	Mario->x=20;
	Mario->y=385;
	ReSet(level1sound);
//	LoopSound(level1sound);
	
}
void CGameMain::Level2()
{
	stage=2;
	EmptyGame();
	map= new CMap(2);
	map->LoadMap();
	camara=new CCamara();
	InitGame();
	timeplay=200;
	Mario->x=35;
	Mario->y=0;
	ReSet(level2sound);
//	LoopSound(level2sound);
}
void CGameMain::Introduce()
{
	if(stage==1) StopSound(level1sound);
	else StopSound(level2sound);
	d3ddv->ColorFill(backbuffer,NULL,D3DCOLOR_XRGB(0,0,0));
	number->RenderIntro(sprite,stage,Mario->life);
	text->RenderIntro(sprite);
	Mario->Render(sprite,0,0);
	
}
void CGameMain::GameOver()
{
	if(stage==1) StopSound(level1sound);
	else StopSound(level2sound);
	d3ddv->ColorFill(backbuffer,NULL,D3DCOLOR_XRGB(0,0,0));
	text->RenderGameOver(sprite);
}
void CGameMain::RePlay()
{
	Mario->RePlay();
	if(stage==1)
	Level1();
	else Level2();
}
void CGameMain::RenderFrame(LPDIRECT3DDEVICE9 d3ddv, int time)
{
	if(stage==2) d3ddv->ColorFill(backbuffer,NULL,D3DCOLOR_XRGB(0,0,0));
	else d3ddv->ColorFill(backbuffer,NULL,D3DCOLOR_XRGB(102,153,255));
	
	sprite->Begin(D3DXSPRITE_ALPHABLEND);
	
	if(Mario->isgameover==true) GameOver();
	else
		if(Mario->isintroduce==true||Mario->isfinish==true) Introduce();// vẽ cái chuyển màn hoặc chơi lại hoặc game over
	else
	{
		if(Mario->isreplay==true) RePlay();
		if(Mario->is_up_level==true)
		{
			FinishGame=NULL;
			Mario->UpLevel();
			Level2();
			Mario->is_up_level=false;
		}
		if(Mario->automario==true||Mario->isdied==true) 
		{
			if(stage==1) StopSound(level1sound);
			else StopSound(level2sound);
		}
	map->GetObjectFromQuadTree(list,camara->getRECT());	
	getListObject();
	
	DWORD now=GetTickCount(); 
	
	
	if(now-last_time>1000)
	{
		if(isMenu==false) timeplay--;
		last_time=now;
	}
	if(timeplay<0&&isMenu==false) 
	{
		Mario->id=3;
		Mario->isdied=true;
		timeplay=300;
	}
	for(int i=0;i<nSights;i++) ArraySights[i]->Render(sprite,image_for_sights,time,camara->xcamara);
	Enemies->Move(StaticObjects,camara->getRECT());
	Enemies->Render(sprite,image_for_enemies,time,camara->xcamara);
	StaticObjects->Render(sprite,image_for_static_objects,camara->getRECT(),time,camara->xcamara);	
	items->Render(sprite,image_for_items,time,camara->xcamara);
	items->Move(StaticObjects,camara->getRECT());
	Mario->Collision(StaticObjects,Enemies,items,FinishGame,camara->getRECT());
	Mario->Render(sprite,time,camara->xcamara);

	if(FinishGame!=NULL) FinishGame->Render(sprite,image_for_static_objects,time,camara->xcamara);
	

	camara->Update(Mario->x,Mario->y);
	EmptyList();

	}
	text->Render(d3ddv,sprite,isMenu,stage);
	if(isMenu==false) number->Render(sprite,timeplay,Mario->score,Mario->number_coins,stage);// vẽ điểm, số tiền, màn chơi, thời gian
	if(isMenu==true) Mushroom->Render(d3ddv,sprite,image_for_menu,index);
	sprite->End();
	
}
void CGameMain::ProcessInput(LPDIRECT3DDEVICE9 d3ddv, int time)
{
	if(Mario!=NULL&&(Mario->id==3||Mario->isfinish==true||Mario->isduck==true)) return;
	if(IsKeyDown(DIK_RIGHT)) 
	{
		Mario->MoveRight();
		if(IsKeyDown(DIK_Z)) Mario->isfast=true;
	}
	else if(IsKeyDown(DIK_LEFT)) Mario->MoveLeft();
	else if(IsKeyDown(DIK_DOWN)) Mario->isduck=true;
}
void CGameMain::OnKeyDown(int KeyCode)
{
	if(Mario==NULL||Mario->id==3) return;
	if((Mario->isintroduce==true||Mario->isfinish==true)&&(Mario->isgameover==false)) return;
	if(KeyCode==DIK_X&&(Mario->isjumping==false&&Mario->isfalling==false&&Mario->vy==0))
	{
		Mario->Jump(); // Gọi pt Jump() để bật cờ nhảy.
	}
	if(KeyCode==DIK_Z&&Mario->id==2&&Mario->isduck==false) Mario->Fire();
	if(KeyCode==DIK_UP)
	{
		if(isMenu==true)
		{
			index--;
			if(index<0) 
			{
				if(stage==0)	index=2;
				else index=4;
			}
			PlaySound(menuchoose);
		}
	}
	if(KeyCode==DIK_DOWN)
	{
		if(isMenu==true)
			{
				index++;
				if(stage==0)
				{
					if(index>2) index=0;
				}
				else if(index>4) index=0;
				PlaySound(menuchoose);
			}
		else Mario->isduck=true;
	}
	if(KeyCode==DIK_SPACE)
	{
		if(isMenu==true)
		{
		switch(index)
		{
		case 0:
			{ 
				isMenu=false;
				Level1();
			}break;
		case 1:{
					if(stage==0) LoadGame();
					else ResumeGame();
			   }break;
		case 2:{
					if(stage==0) PostMessage(hWnd,WM_QUIT,0,0);
					else SaveGame();
			   }break;
		case 3:{
					LoadGame();
			   }break;
		case 4:	PostMessage(hWnd,WM_QUIT,0,0);break;
		}
		}
		index=0;
	}
	if(KeyCode==DIK_ESCAPE&&stage!=0) 
	{
		
		PauseGame();
		if(Mario->isgameover==true) MenuStage();
		isMenu=true;
		if(stage==1) StopSound(level1sound);	
		else StopSound(level2sound);
	}
	//else if(KeyCode==DIK_RIGHT) Mario->MoveRight();
}
void CGameMain::OnKeyUp(int KeyCode)
{
	if(KeyCode==DIK_DOWN) Mario->isduck=false;
//	else if(KeyCode==DIK_Z) Mario->isfire=false;
		else Mario->Stand();
	if(KeyCode==DIK_X) 
	{
		Mario->jump=false;
		Mario->isfalling=true;
	}
}
void CGameMain::PauseGame()
{
	Mario->ispause=true;
	Enemies->ispause=true;
}
void CGameMain::ResumeGame()
{
	Mario->ispause=false;
	Enemies->ispause=false;
	isMenu=false;
	if(stage==1) LoopSound(level1sound);
	else LoopSound(level2sound);
}
void CGameMain::SaveGame()
{
	ofstream file("Save\\GameMain.AHNT", ios::out);
	file<<stage<<"\n";
	file<<timeplay<<"\n";
	//file<<camara->xcamara<<"\n";*/
	Mario->SaveGame();
}
void CGameMain::LoadGame()
{
	ifstream inFile;
	inFile.open("Save\\GameMain.AHNT");
	inFile>>stage;
	inFile>>timeplay;
//	inFile>>camara->xcamara;
	inFile.close();
	EmptyGame();
	map= new CMap(stage);
	map->LoadMap();
	InitGame();
	Mario->LoadGame();
	ResumeGame();
}